<template>
  <div class="box bgc3 sku-sale-rank">
    <div class="header">
      <div class="title">
        租赁热榜<span class="sub-title">{{ start }} ~ {{ end }}</span>
      </div>
    </div>
    <div v-if="loading" class="loading-state">加载中...</div>
    <div v-else-if="error" class="error-state">数据加载失败</div>
    <div v-else class="body">
      <el-row v-for="(item, index) in skuSaleRank" :key="index">
        <el-col :span="5">
          <div :class="'top top' + (index + 1)">
            {{ index + 1 }}
          </div>
        </el-col>
        <el-col :span="13">
          <div class="name" :title="item.skuName">
            {{ item.name }}
          </div>
        </el-col>
        <el-col :span="6">
          <div class="count">{{ item.count }}单</div>
        </el-col>
      </el-row>

    </div>
  </div>
  
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getRankChartData } from "@/api/earch/rank";
import dayjs from 'dayjs'

const skuSaleRank = ref([])
const start = ref(dayjs().startOf('month').format('YYYY.MM.DD'))
const end = ref(dayjs().endOf('month').format('YYYY.MM.DD')) // 改为当月结束
const loading = ref(true)
const error = ref(false)

onMounted(async () => {
  try {
    const response = await getRankChartData()
    
  skuSaleRank.value = response // 根据实际返回结构调整
  } catch (e) {
    error.value = true
    console.error('数据加载失败', e)
  } finally {
    loading.value = false
  }
})
</script>

<style lang="scss" scoped>
@import '@/assets/styles/variables.module.scss';

.sku-sale-rank {
  display: flex;
  flex-direction: column;
  height: calc((100vh - 120px) * 0.6);
  min-height: 538px;
  background: #FFFFFF;
  border-radius: 20px;

  .body {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-top: 20px;
    .top {
      display: inline-block;
      width: 16px;
      height: 20px;
      margin-left: 10px;
      background: url('@/assets/user-task-stats/top.png');
      text-align: center;
      font-size: 12px;
      font-weight: normal;
      color: #E9B499;
      line-height: 14px;
    }
    .top1 {
      width: 21px;
      height: 20px;
      background: url('@/assets/user-task-stats/top1.png');
      color: #8E5900;
    }
    .top2 {
      width: 21px;
      height: 20px;
      background: url('@/assets/user-task-stats/top2.png');
      color: #494949;
    }
    .top3 {
      width: 21px;
      height: 20px;
      background: url('@/assets/user-task-stats/top3.png');
      color: #CF6D3D;
    }
    .name {
      height: 20px;
      font-size: 14px;
      font-weight: 500;
      color: $--color-text-primary;
      line-height: 20px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    .count {
      height: 20px;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #737589;
      line-height: 20px;
      text-align: right;
    }
  }
}
</style>
